<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>React路由 | 小马の博客</title><meta name="keywords" content="React 路由"><meta name="author" content="Kun Kun"><meta name="copyright" content="Kun Kun"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一、react 路由路由基本信息 1、安装 react-router-domyarn add react-router-dom --save 二、路由的基本使用1.明确好界面中的导航区、展示区 2.导航区的a标签改为Link标签             &#96;&lt;Link to&#x3D;&quot;&#x2F;xxxxx&quot;&gt;Demo&lt;&#x2F;Link&gt;&#96; 3.展示区写Route标签进行路径的匹">
<meta property="og:type" content="article">
<meta property="og:title" content="React路由">
<meta property="og:url" content="https://ma_jinkun.gitee.io/2022/01/04/react-ly/index.html">
<meta property="og:site_name" content="小马の博客">
<meta property="og:description" content="一、react 路由路由基本信息 1、安装 react-router-domyarn add react-router-dom --save 二、路由的基本使用1.明确好界面中的导航区、展示区 2.导航区的a标签改为Link标签             &#96;&lt;Link to&#x3D;&quot;&#x2F;xxxxx&quot;&gt;Demo&lt;&#x2F;Link&gt;&#96; 3.展示区写Route标签进行路径的匹">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://image-tupss.test.upcdn.net/baner.png">
<meta property="article:published_time" content="2022-01-04T06:28:11.000Z">
<meta property="article:modified_time" content="2022-01-06T06:32:20.759Z">
<meta property="article:author" content="Kun Kun">
<meta property="article:tag" content="React 路由">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://image-tupss.test.upcdn.net/baner.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://ma_jinkun.gitee.io/2022/01/04/react-ly/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'React路由',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-01-06 14:32:20'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper/swiper/swiper.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper/swiper/swiperstyle.css"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s2.loli.net/2021/12/28/uBIDZUEFrpsCvH7.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">8</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音樂</span></a></div><div class="menus_item"><a class="site-page" href="/contact/"><i class="fa-fw far fa-comments"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s2.loli.net/2021/12/30/OjiswQgfeP3Jm4S.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小马の博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音樂</span></a></div><div class="menus_item"><a class="site-page" href="/contact/"><i class="fa-fw far fa-comments"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">React路由</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-01-04T06:28:11.000Z" title="发表于 2022-01-04 14:28:11">2022-01-04</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-06T06:32:20.759Z" title="更新于 2022-01-06 14:32:20">2022-01-06</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/React%E5%AD%A6%E4%B9%A0-%E8%B7%AF%E7%94%B1/">React学习  路由</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="React路由"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="一、react-路由"><a href="#一、react-路由" class="headerlink" title="一、react 路由"></a>一、react 路由</h2><p>路由基本信息</p>
<p>1、安装 react-router-dom<br><code>yarn add react-router-dom --save</code></p>
<h2 id="二、路由的基本使用"><a href="#二、路由的基本使用" class="headerlink" title="二、路由的基本使用"></a>二、路由的基本使用</h2><pre><code>1.明确好界面中的导航区、展示区
2.导航区的a标签改为Link标签
            `&lt;Link to=&quot;/xxxxx&quot;&gt;Demo&lt;/Link&gt;`
3.展示区写Route标签进行路径的匹配
            `&lt;Route path=&#39;/xxxx&#39; component=&#123;Demo&#125;/&gt;`
4.&lt;App&gt;的最外侧包裹了一个`&lt;BrowserRouter&gt;`或`&lt;HashRouter&gt;`
</code></pre>
<h2 id="三、路由组件与一般组件"><a href="#三、路由组件与一般组件" class="headerlink" title="三、路由组件与一般组件"></a>三、路由组件与一般组件</h2><blockquote>
<p>各个生命周期钩子调用顺序</p>
<ol>
<li> 写法不同</li>
</ol>
<p>一般组件: <code>&lt;Demo&gt;</code><br>路由组件: <code> &lt;Route path=&quot;/about&quot; component=&#123;About&#125;/&gt;</code></p>
<ol start="2">
<li> 存放位置不同</li>
</ol>
<p>一般组件: <code>component文件夹</code><br>路由组件: <code> pages文件夹</code></p>
<ol start="3">
<li><p>接受到的 props 不同</p>
<pre><code> 一般组件: 写组件的标签传递了什么就接受到了什么
 路由组件: 接受到 3 个固定的属性

 `history:
 go: ƒ go(n)
 goBack: ƒ goBack()
 goForward: ƒ goForward()
 push: ƒ push(path, state)
 replace: ƒ replace(path, state)
 location:
 pathname: &quot;/about&quot;
 search: &quot;&quot;
 state: undefined
 match:
 params: &#123;&#125;
 path: &quot;/about&quot;
 url: &quot;/about&quot; `
</code></pre>
</li>
</ol>
</blockquote>
<h2 id="四、NavLink与封装NavLink"><a href="#四、NavLink与封装NavLink" class="headerlink" title="四、NavLink与封装NavLink"></a>四、NavLink与封装NavLink</h2><blockquote>
<pre><code>    1.NavLink可以实现路由链接的高亮,通过 `activeClassName`指定样式
    2.标签体的内容是一个特殊的标签属性
  3.通过`this.props.children`可以获取到标签体里面的内容
</code></pre>
</blockquote>
<h3 id="封装NavLink代码的代码"><a href="#封装NavLink代码的代码" class="headerlink" title="封装NavLink代码的代码"></a>封装NavLink代码的代码</h3><blockquote>
<p> 1.在component里面创建一个 <code>Mynavlink</code> 组件进行封装 NavLink</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123; Component &#125; from &#x27;react&#x27;</span><br><span class="line">  import &#123;NavLink&#125; from &#x27;react-router-dom&#x27;  //引入NavLink</span><br><span class="line">  export default class Mynavlink extends Component &#123;</span><br><span class="line">    render() &#123;</span><br><span class="line">        console.log(this.props)</span><br><span class="line">        // const &#123;to,title&#125; = this.props</span><br><span class="line">        return (</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">                  &lt;NavLink activeClassName=&quot;atguigu&quot; className=&quot;list-group-item&quot; &#123;...this.props&#125;/&gt;</span><br><span class="line">                 &lt;!-- &lt;NavLink activeClassName=&quot;atguigu&quot; className=&quot;list-group-item&quot; to=&#123;to&#125; &gt;&#123;title&#125;&lt;/    NavLink&gt;  --&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<p>2.引入Mynavlink这组件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123; Component &#125; from &#x27;react&#x27;</span><br><span class="line">import &#123;NavLink,Route&#125; from &#x27;react-router-dom&#x27;</span><br><span class="line">import Home from &#x27;./pages/Home&#x27; //Home是路由组件</span><br><span class="line">import About from &#x27;./pages/About&#x27; //About是路由组件</span><br><span class="line">import Mynavlink from &#x27;./components/Mynavlink&#x27; //引入Mynavlink这组件</span><br><span class="line">export default class App extends Component &#123;</span><br><span class="line">render() &#123;</span><br><span class="line">	return (</span><br><span class="line">		&lt;div&gt;</span><br><span class="line">			&lt;div className=&quot;row&quot;&gt;</span><br><span class="line">				&lt;div className=&quot;col-xs-offset-2 col-xs-8&quot;&gt;</span><br><span class="line">					&lt;Header/&gt;</span><br><span class="line">				&lt;/div&gt;</span><br><span class="line">			&lt;/div&gt;</span><br><span class="line">			&lt;div className=&quot;row&quot;&gt;</span><br><span class="line">				&lt;div className=&quot;col-xs-2 col-xs-offset-2&quot;&gt;</span><br><span class="line">					&lt;div className=&quot;list-group&quot;&gt;</span><br><span class="line">						&#123;/* activeClassName:添加样式  */&#125;</span><br><span class="line">						&#123;/* 接受标签体内容  在children里面 */&#125;</span><br><span class="line">						&lt;Mynavlink to=&quot;/about&quot; a=&quot;a&quot;  b=&quot;b&quot; &gt;About&lt;/Mynavlink&gt;</span><br><span class="line">						&lt;Mynavlink to=&quot;/home&quot;&gt;Home&lt;/Mynavlink&gt;</span><br><span class="line">					&lt;/div&gt;</span><br><span class="line">				&lt;/div&gt;</span><br><span class="line">				&lt;div className=&quot;col-xs-6&quot;&gt;</span><br><span class="line">					&lt;div className=&quot;panel&quot;&gt;</span><br><span class="line">						&lt;div className=&quot;panel-body&quot;&gt;</span><br><span class="line">							&#123;/* 注册路由 */&#125;</span><br><span class="line">							&lt;Route path=&quot;/about&quot; component=&#123;About&#125;/&gt;</span><br><span class="line">							&lt;Route path=&quot;/home&quot; component=&#123;Home&#125;/&gt;</span><br><span class="line">						&lt;/div&gt;</span><br><span class="line">					&lt;/div&gt;</span><br><span class="line">				&lt;/div&gt;</span><br><span class="line">			&lt;/div&gt;</span><br><span class="line">		&lt;/div&gt;</span><br><span class="line">	)</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="五、Switch的使用"><a href="#五、Switch的使用" class="headerlink" title="五、Switch的使用"></a>五、Switch的使用</h2><blockquote>
<p>   1.通常情况想,path与component是一一对应的关系<br>   2.Switch可以提高路由的匹配效率(单一匹配)</p>
</blockquote>
<blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">1.引入Switch</span><br><span class="line">`import &#123;Route,Switch&#125; from &#x27;react-router-dom&#x27;`</span><br><span class="line">2.包着注册路由</span><br><span class="line">&#123;/* 注册路由 */&#125;</span><br><span class="line">&lt;Switch&gt;</span><br><span class="line">&lt;Route path=&quot;/about&quot; component=&#123;About&#125;/&gt;</span><br><span class="line">&lt;Route path=&quot;/home&quot; component=&#123;Home&#125;/&gt;</span><br><span class="line">&lt;Route path=&quot;/home&quot; component=&#123;Text&#125;/&gt;</span><br><span class="line">&lt;/Switch&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="六、解决多级路径刷新页面样式丢失的问题"><a href="#六、解决多级路径刷新页面样式丢失的问题" class="headerlink" title="六、解决多级路径刷新页面样式丢失的问题"></a>六、解决多级路径刷新页面样式丢失的问题</h2><blockquote>
<ol>
<li>public/index.html 中 引入样式时不写 ./ 写 / （常用）</li>
<li>public/index.html 中 引入样式时不写 ./ 写 %PUBLIC_URL% （常用）</li>
<li>使用HashRouter</li>
</ol>
</blockquote>
<blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">export default class App extends Component &#123;</span><br><span class="line">render() &#123;</span><br><span class="line">	return (</span><br><span class="line">		&lt;div&gt;</span><br><span class="line">			&lt;div className=&quot;row&quot;&gt;</span><br><span class="line">				&lt;div className=&quot;col-xs-2 col-xs-offset-2&quot;&gt;</span><br><span class="line">					&lt;div className=&quot;list-group&quot;&gt;</span><br><span class="line">						&#123;/* 在进行路由的重定向 `/ATC/` 的时候 刷新页面可能会样式丢失的问题 */&#125;</span><br><span class="line">						&lt;Mynavlink to=&quot;/ATC/about&quot; a=&quot;a&quot;  b=&quot;b&quot; &gt;About&lt;/Mynavlink&gt;</span><br><span class="line">						&lt;Mynavlink to=&quot;/ATC/home&quot;&gt;Home&lt;/Mynavlink&gt;</span><br><span class="line">					&lt;/div&gt;</span><br><span class="line">				&lt;/div&gt;</span><br><span class="line">				&lt;div className=&quot;col-xs-6&quot;&gt;</span><br><span class="line">					&lt;div className=&quot;panel&quot;&gt;</span><br><span class="line">						&lt;div className=&quot;panel-body&quot;&gt;</span><br><span class="line">							&#123;/* 注册路由 */&#125;</span><br><span class="line">							&lt;Switch&gt;</span><br><span class="line">							&lt;Route path=&quot;/ATC/about&quot; component=&#123;About&#125;/&gt;</span><br><span class="line">							&lt;Route path=&quot;/ATC/home&quot; component=&#123;Home&#125;/&gt;</span><br><span class="line">							&lt;/Switch&gt;</span><br><span class="line">						&lt;/div&gt;</span><br><span class="line">					&lt;/div&gt;</span><br><span class="line">				&lt;/div&gt;</span><br><span class="line">			&lt;/div&gt;</span><br><span class="line">		&lt;/div&gt;</span><br><span class="line">	)</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="七、路由的严格匹配与模糊匹配"><a href="#七、路由的严格匹配与模糊匹配" class="headerlink" title="七、路由的严格匹配与模糊匹配"></a>七、路由的严格匹配与模糊匹配</h2><blockquote>
<p>1.默认使用的是模糊匹配（简单记：【输入的路径】必须包含要【匹配的路径】，且顺序要一致）<br>2.开启严格匹配：<Route exact={true} path="/about" component={About}/><br>3.严格匹配不要随便开启，需要再开，有些时候开启会导致无法继续匹配二级路由</p>
</blockquote>
<blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;div className=&quot;col-xs-6&quot;&gt;</span><br><span class="line">&lt;div className=&quot;panel&quot;&gt;</span><br><span class="line">	&lt;div className=&quot;panel-body&quot;&gt;</span><br><span class="line">		&#123;/* 注册路由 */&#125;</span><br><span class="line">		&#123;/* exact 不开启的话  */&#125;</span><br><span class="line">		&lt;Switch&gt;</span><br><span class="line">		&lt;Route exact path=&quot;/about&quot; component=&#123;About&#125;/&gt;</span><br><span class="line">		&lt;Route exact path=&quot;/home&quot; component=&#123;Home&#125;/&gt;</span><br><span class="line">		&lt;/Switch&gt;</span><br><span class="line">	&lt;/div&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="八、Redirect的使用–路由的重定向"><a href="#八、Redirect的使用–路由的重定向" class="headerlink" title="八、Redirect的使用–路由的重定向"></a>八、Redirect的使用–路由的重定向</h2><blockquote>
<p>   1.一般写在所有路由注册的最下方，当所有路由都无法匹配时，跳转到Redirect指的路由<br>   2.具体编码：</p>
</blockquote>
<blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;Switch&gt;</span><br><span class="line">&lt;Route path=&quot;/about&quot; component=&#123;About&#125;/&gt;</span><br><span class="line">&lt;Route path=&quot;/home&quot; component=&#123;Home&#125;/&gt;</span><br><span class="line">&lt;Redirect to=&quot;/about&quot;/&gt;  //路由的重定向到about</span><br><span class="line">&lt;/Switch&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="九、嵌套路由"><a href="#九、嵌套路由" class="headerlink" title="九、嵌套路由"></a>九、嵌套路由</h2><blockquote>
<p>   1.注册子路由时要写上父路由的path值<br>   2.路由的匹配是按照注册路由的顺序进行的</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&lt;div&gt;</span><br><span class="line">       &lt;h3&gt;我是Home的内容&lt;/h3&gt;</span><br><span class="line">       &lt;div&gt;</span><br><span class="line">           &lt;ul className=&quot;nav nav-tabs&quot;&gt;</span><br><span class="line">             &lt;li&gt;</span><br><span class="line">				&lt;Mynavlink to=&quot;/home/news&quot; &gt;News&lt;/Mynavlink&gt;</span><br><span class="line">             &lt;/li&gt;</span><br><span class="line">             &lt;li&gt;</span><br><span class="line">             		&lt;Mynavlink to=&quot;/home/message&quot; &gt;Message&lt;/Mynavlink&gt;</span><br><span class="line">             &lt;/li&gt;</span><br><span class="line">           &lt;/ul&gt; </span><br><span class="line">           &lt;div&gt;</span><br><span class="line">		  &#123;/* 注册路由 */&#125;</span><br><span class="line">		  &lt;Switch&gt;</span><br><span class="line">		  	&lt;Route path=&quot;/home/news&quot; component=&#123;News&#125; /&gt;</span><br><span class="line">		  	&lt;Route path=&quot;/home/message&quot; component=&#123;Message&#125; /&gt;</span><br><span class="line">		 	&lt;Redirect to=&quot;/home/news&quot;/&gt;</span><br><span class="line">		  	&lt;/Switch&gt;</span><br><span class="line">           &lt;/div&gt;</span><br><span class="line">       &lt;/div&gt;</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="十、向路由组件传递参数"><a href="#十、向路由组件传递参数" class="headerlink" title="十、向路由组件传递参数"></a>十、向路由组件传递参数</h2><h3 id="1-通过params参数传递"><a href="#1-通过params参数传递" class="headerlink" title="1.通过params参数传递"></a>1.通过params参数传递</h3><blockquote>
<p>   路由链接(携带参数)<br><code>&lt;Link to=&#123;</code>/home/message/detail/${msobj.id}/${msobj.title}<code>&#125; &#123;msobj.title&#125; &lt;/Link&gt;</code></p>
<p>注册路由(声明接收)：<br><code>&lt;Route path=&quot;/demo/test/:name/:age&quot; component=&#123;Test&#125;/&gt;</code></p>
<p>接收参数：this.props.match.params</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">export default class Message extends Component &#123;</span><br><span class="line"> state = &#123;</span><br><span class="line">   messageArr:[</span><br><span class="line">     &#123;id:&#x27;01&#x27;,title:&#x27;消息1&#x27;&#125;,</span><br><span class="line">     &#123;id:&#x27;02&#x27;,title:&#x27;消息2&#x27;&#125;,</span><br><span class="line">     &#123;id:&#x27;03&#x27;,title:&#x27;消息3&#x27;&#125;</span><br><span class="line">   ]</span><br><span class="line"> &#125;</span><br><span class="line">   render() &#123;</span><br><span class="line">     const &#123;messageArr&#125;  =this.state</span><br><span class="line">       return (</span><br><span class="line">           &lt;div&gt;</span><br><span class="line">               &lt;div&gt;</span><br><span class="line">                 &lt;ul&gt; </span><br><span class="line">                   &#123;</span><br><span class="line">                     messageArr.map( (msobj)=&gt;&#123;</span><br><span class="line">                       return (</span><br><span class="line">                         &lt;li key=&#123;msobj.id&#125; &gt;</span><br><span class="line">                           &#123;/* 1.像路由组件传递params参数  */&#125;</span><br><span class="line">                           &lt;Link to=&#123;`/home/message/detail/$&#123;msobj.id&#125;/$&#123;msobj.title&#125;`&#125; &gt; &#123;msobj.title&#125; &lt;/Link&gt;</span><br><span class="line">		           &lt;!-- 相当于下面这种格式 --&gt;</span><br><span class="line">		           &lt;!--  &lt;Link to=&#x27;/home/message/detail/19&#x27; &gt; &#123;msobj.title&#125; &lt;/Link&gt; --&gt;</span><br><span class="line">                         &lt;/li&gt;</span><br><span class="line">                       )</span><br><span class="line">                     &#125;)</span><br><span class="line">                   &#125;</span><br><span class="line">                 &lt;/ul&gt;</span><br><span class="line">               &lt;/div&gt;</span><br><span class="line"></span><br><span class="line">               &#123;/* 1. 声明接受params参数  那边接收参数：this.props.match.params */&#125;</span><br><span class="line">               &lt;Route path=&quot;/home/message/detail/:id/:title&quot; component=&#123;Detail&#125;/&gt;</span><br><span class="line">               </span><br><span class="line">           &lt;/div&gt;</span><br><span class="line">       )</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</blockquote>
<h3 id="2-search参数"><a href="#2-search参数" class="headerlink" title="2.search参数"></a>2.search参数</h3><blockquote>
<p>   路由链接(携带参数)<br><code> &lt;Link to=&#123;</code>/home/message/detail/?id=${msobj.id}&amp;title=${msobj.title}<code>&#125; &gt; &#123;msobj.title&#125; &lt;/Link&gt;</code></p>
<p>注册路由(声明接收)：声明接受search参数—无需要声明接受 正常接受<br><code>&lt;Route path=&quot;/home/message/detail&quot; component=&#123;Detail&#125;/&gt;</code></p>
<p>接收参数：this.props.location.search</p>
<p>备注：获取到的search是urlencoded编码字符串，需要借助 querystring 解析</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">我这里的search是这样的 使用需要需要借助 querystring 解析</span><br><span class="line"></span><br><span class="line">search: &quot;?id=01&amp;title=消息1&quot;</span><br><span class="line">代码 如下：</span><br><span class="line">import qs from &#x27;query-string&#x27;  //引入 下载 npm i -save-dev query-string</span><br><span class="line">const &#123; search&#125; = this.props.location</span><br><span class="line">const &#123; id,title&#125; = qs.parse(search.slice(1))</span><br></pre></td></tr></table></figure>
</blockquote>
<h3 id="3-state参数"><a href="#3-state参数" class="headerlink" title="3.state参数"></a>3.state参数</h3><blockquote>
<p>   路由链接(携带参数)<br><code>&lt;Link to=&#123;&#123;pathname:'/home/message/detail',state: &#123;id:msobj.id,title:msobj.title&#125;&#125;&#125;&gt; &#123;msobj.title&#125; &lt;/Link&gt;</code></p>
<p>注册路由(声明接收)：声明接受search参数—无需要声明接受 正常接受<br><code>&lt;Route path=&quot;/home/message/detail&quot; component=&#123;Detail&#125;/&gt;</code></p>
<p>接收参数：this.props.location.state</p>
<p>备注：刷新也可以保留住参数</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="十一、编程式路由导航"><a href="#十一、编程式路由导航" class="headerlink" title="十一、编程式路由导航"></a>十一、编程式路由导航</h2><blockquote>
<p>   借助this.prosp.history对象上的API对操作路由跳转、前进、后退<br>           -this.prosp.history.push()<br>           -this.prosp.history.replace()<br>           -this.prosp.history.goBack()<br>           -this.prosp.history.goForward()<br>           -this.prosp.history.go()</p>
</blockquote>
<h2 id="十二、BrowserRouter与HashRouter的区别"><a href="#十二、BrowserRouter与HashRouter的区别" class="headerlink" title="十二、BrowserRouter与HashRouter的区别"></a>十二、BrowserRouter与HashRouter的区别</h2><blockquote>
<pre><code>       1.底层原理不一样：
                   BrowserRouter使用的是H5的history API，不兼容IE9及以下版本。
                   HashRouter使用的是URL的哈希值。
       2.path表现形式不一样
                   BrowserRouter的路径中没有#,例如：localhost:3000/demo/test
                   HashRouter的路径包含#,例如：localhost:3000/#/demo/test
       3.刷新后对路由state参数的影响
                   (1).BrowserRouter没有任何影响，因为state保存在history对象中。
                   (2).HashRouter刷新后会导致路由state参数的丢失！！！
       4.备注：HashRouter可以用于解决一些路径错误相关的问题。
</code></pre>
</blockquote>
<h2 id="十三、antd的按需引入-自定主题"><a href="#十三、antd的按需引入-自定主题" class="headerlink" title="十三、antd的按需引入+自定主题"></a>十三、antd的按需引入+自定主题</h2><pre><code>        1.安装依赖：yarn add react-app-rewired customize-cra babel-plugin-import less less-loader
</code></pre>
<blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">2.修改package.json</span><br><span class="line">	....</span><br><span class="line">		&quot;scripts&quot;: &#123;</span><br><span class="line">			&quot;start&quot;: &quot;react-app-rewired start&quot;,</span><br><span class="line">			&quot;build&quot;: &quot;react-app-rewired build&quot;,</span><br><span class="line">			&quot;test&quot;: &quot;react-app-rewired test&quot;,</span><br><span class="line">			&quot;eject&quot;: &quot;react-scripts eject&quot;</span><br><span class="line">		&#125;,</span><br><span class="line">	....</span><br><span class="line">3.根目录下创建config-overrides.js</span><br><span class="line">	//配置具体的修改规则</span><br><span class="line">	const &#123; override, fixBabelImports,addLessLoader&#125; = require(&#x27;customize-cra&#x27;);</span><br><span class="line">	module.exports = override(</span><br><span class="line">		fixBabelImports(&#x27;import&#x27;, &#123;</span><br><span class="line">			libraryName: &#x27;antd&#x27;,</span><br><span class="line">			libraryDirectory: &#x27;es&#x27;,</span><br><span class="line">			style: true,</span><br><span class="line">		&#125;),</span><br><span class="line">		addLessLoader(&#123;</span><br><span class="line">			lessOptions:&#123;</span><br><span class="line">				javascriptEnabled: true,</span><br><span class="line">				modifyVars: &#123; &#x27;@primary-color&#x27;: &#x27;green&#x27; &#125;,</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;),</span><br><span class="line">	);</span><br></pre></td></tr></table></figure>
<pre><code>4.备注：不用在组件里亲自引入样式了，即：import &#39;antd/dist/antd.css&#39;应该删掉
</code></pre>
</blockquote>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Kun Kun</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://ma_jinkun.gitee.io/2022/01/04/react-ly/">https://ma_jinkun.gitee.io/2022/01/04/react-ly/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://ma_jinkun.gitee.io" target="_blank">小马の博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/React-%E8%B7%AF%E7%94%B1/">React 路由</a></div><div class="post_share"><div class="social-share" data-image="[&quot;https://image-tupss.test.upcdn.net/baner.png&quot;]" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="next-post pull-full"><a href="/2021/12/30/react/react-proxy/"><img class="next-cover" src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">React 配置代理</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s2.loli.net/2021/12/28/uBIDZUEFrpsCvH7.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Kun Kun</div><div class="author-info__description">嘘,别看,眼睛会疼！</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">8</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/xxxxx" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2583931485@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="qq:2583931485" target="_blank" title="QQ"><i class="fab fa-qq"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">基于Hexo 主题butterfly 开发的</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81react-%E8%B7%AF%E7%94%B1"><span class="toc-number">1.</span> <span class="toc-text">一、react 路由</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E8%B7%AF%E7%94%B1%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">二、路由的基本使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E8%B7%AF%E7%94%B1%E7%BB%84%E4%BB%B6%E4%B8%8E%E4%B8%80%E8%88%AC%E7%BB%84%E4%BB%B6"><span class="toc-number">3.</span> <span class="toc-text">三、路由组件与一般组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81NavLink%E4%B8%8E%E5%B0%81%E8%A3%85NavLink"><span class="toc-number">4.</span> <span class="toc-text">四、NavLink与封装NavLink</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%81%E8%A3%85NavLink%E4%BB%A3%E7%A0%81%E7%9A%84%E4%BB%A3%E7%A0%81"><span class="toc-number">4.1.</span> <span class="toc-text">封装NavLink代码的代码</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81Switch%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">5.</span> <span class="toc-text">五、Switch的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E8%A7%A3%E5%86%B3%E5%A4%9A%E7%BA%A7%E8%B7%AF%E5%BE%84%E5%88%B7%E6%96%B0%E9%A1%B5%E9%9D%A2%E6%A0%B7%E5%BC%8F%E4%B8%A2%E5%A4%B1%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-number">6.</span> <span class="toc-text">六、解决多级路径刷新页面样式丢失的问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%83%E3%80%81%E8%B7%AF%E7%94%B1%E7%9A%84%E4%B8%A5%E6%A0%BC%E5%8C%B9%E9%85%8D%E4%B8%8E%E6%A8%A1%E7%B3%8A%E5%8C%B9%E9%85%8D"><span class="toc-number">7.</span> <span class="toc-text">七、路由的严格匹配与模糊匹配</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AB%E3%80%81Redirect%E7%9A%84%E4%BD%BF%E7%94%A8%E2%80%93%E8%B7%AF%E7%94%B1%E7%9A%84%E9%87%8D%E5%AE%9A%E5%90%91"><span class="toc-number">8.</span> <span class="toc-text">八、Redirect的使用–路由的重定向</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B9%9D%E3%80%81%E5%B5%8C%E5%A5%97%E8%B7%AF%E7%94%B1"><span class="toc-number">9.</span> <span class="toc-text">九、嵌套路由</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%81%E3%80%81%E5%90%91%E8%B7%AF%E7%94%B1%E7%BB%84%E4%BB%B6%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0"><span class="toc-number">10.</span> <span class="toc-text">十、向路由组件传递参数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E9%80%9A%E8%BF%87params%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92"><span class="toc-number">10.1.</span> <span class="toc-text">1.通过params参数传递</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-search%E5%8F%82%E6%95%B0"><span class="toc-number">10.2.</span> <span class="toc-text">2.search参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-state%E5%8F%82%E6%95%B0"><span class="toc-number">10.3.</span> <span class="toc-text">3.state参数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%81%E4%B8%80%E3%80%81%E7%BC%96%E7%A8%8B%E5%BC%8F%E8%B7%AF%E7%94%B1%E5%AF%BC%E8%88%AA"><span class="toc-number">11.</span> <span class="toc-text">十一、编程式路由导航</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%81%E4%BA%8C%E3%80%81BrowserRouter%E4%B8%8EHashRouter%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">12.</span> <span class="toc-text">十二、BrowserRouter与HashRouter的区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%81%E4%B8%89%E3%80%81antd%E7%9A%84%E6%8C%89%E9%9C%80%E5%BC%95%E5%85%A5-%E8%87%AA%E5%AE%9A%E4%B8%BB%E9%A2%98"><span class="toc-number">13.</span> <span class="toc-text">十三、antd的按需引入+自定主题</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/01/04/react-ly/" title="React路由"><img src="[&quot;https://image-tupss.test.upcdn.net/baner.png&quot;]" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="React路由"/></a><div class="content"><a class="title" href="/2022/01/04/react-ly/" title="React路由">React路由</a><time datetime="2022-01-04T06:28:11.000Z" title="发表于 2022-01-04 14:28:11">2022-01-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/30/react/react-proxy/" title="React 配置代理"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="React 配置代理"/></a><div class="content"><a class="title" href="/2021/12/30/react/react-proxy/" title="React 配置代理">React 配置代理</a><time datetime="2021-12-30T02:14:13.000Z" title="发表于 2021-12-30 10:14:13">2021-12-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/30/react/react-cycle/" title="React(生命周期,脚手架)"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="React(生命周期,脚手架)"/></a><div class="content"><a class="title" href="/2021/12/30/react/react-cycle/" title="React(生命周期,脚手架)">React(生命周期,脚手架)</a><time datetime="2021-12-30T01:58:45.000Z" title="发表于 2021-12-30 09:58:45">2021-12-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/30/react/react-two/" title="React第二天(组件三大属性)"><img src="[&quot;https://s2.loli.net/2022/01/05/ezb8QtqIdmLEShW.jpg&quot;]" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="React第二天(组件三大属性)"/></a><div class="content"><a class="title" href="/2021/12/30/react/react-two/" title="React第二天(组件三大属性)">React第二天(组件三大属性)</a><time datetime="2021-12-30T01:49:08.000Z" title="发表于 2021-12-30 09:49:08">2021-12-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/29/react/React-one/" title="React学习"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="React学习"/></a><div class="content"><a class="title" href="/2021/12/29/react/React-one/" title="React学习">React学习</a><time datetime="2021-12-29T09:24:15.000Z" title="发表于 2021-12-29 17:24:15">2021-12-29</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Kun Kun</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function history_calendar_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div>';
                console.log('已挂载history_calendar')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            history_calendar_injector_config()
        } </script><script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script> <script data-pjax>if(document.getElementById('recent-posts') && location.pathname =='/'){
    
    var parent = document.getElementById('recent-posts');
    var child = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" alt="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg"/></div><div class="blog-slider__content"><span class="blog-slider__code">2021-12-28</span><a class="blog-slider__title" href="2021/12/28/My-First-Artical/">第一次测试文档</a><div class="blog-slider__text">简单好用的 hexo 博客文章置顶插件！</div><a class="blog-slider__button" href="2021/12/28/My-First-Artical/">详情</a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://image-tupss.test.upcdn.net/baner.png" alt="https://image-tupss.test.upcdn.net/baner.png"/></div><div class="blog-slider__content"><span class="blog-slider__code">2022-01-04</span><a class="blog-slider__title" href="2022/01/04/react-ly/">React路由</a><div class="blog-slider__text">React路由！</div><a class="blog-slider__button" href="2022/01/04/react-ly/">详情</a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" alt="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg"/></div><div class="blog-slider__content"><span class="blog-slider__code">2021-12-30</span><a class="blog-slider__title" href="2021/12/30/react/react-cycle/">React(生命周期,脚手架)</a><div class="blog-slider__text">生命周期 脚手架！</div><a class="blog-slider__button" href="2021/12/30/react/react-cycle/">详情</a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" alt="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg"/></div><div class="blog-slider__content"><span class="blog-slider__code">2021-12-30</span><a class="blog-slider__title" href="2021/12/30/react/react-proxy/">React 配置代理</a><div class="blog-slider__text">配置代理！</div><a class="blog-slider__button" href="2021/12/30/react/react-proxy/">详情</a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://s2.loli.net/2022/01/05/ezb8QtqIdmLEShW.jpg" alt="https://s2.loli.net/2022/01/05/ezb8QtqIdmLEShW.jpg"/></div><div class="blog-slider__content"><span class="blog-slider__code">2021-12-30</span><a class="blog-slider__title" href="2021/12/30/react/react-two/">React第二天(组件三大属性)</a><div class="blog-slider__text">第二天！</div><a class="blog-slider__button" href="2021/12/30/react/react-two/">详情</a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" alt="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg"/></div><div class="blog-slider__content"><span class="blog-slider__code">2021-12-29</span><a class="blog-slider__title" href="2021/12/29/react/React-one/">React学习</a><div class="blog-slider__text">第一天！</div><a class="blog-slider__button" href="2021/12/29/react/React-one/">详情</a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
    console.log('已挂载swiper')
    parent.insertAdjacentHTML("afterbegin",child)}
     </script>
<script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper/swiper/swiper.min.js"></script>
<script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper@0.18/swiper/swiperindex.js"></script>
<style></style><!-- hexo injector body_end end --></body></html>